package com.sumer.brain.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumer.brain.common.model.R;
import com.sumer.brain.common.utils.EasyExcelUtil;
import com.sumer.brain.sys.model.entity.SysDictType;
import com.sumer.brain.sys.model.req.SysDictTypeReq;
import com.sumer.brain.sys.service.SysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Tag(name = "字典类型管理")
@RequestMapping("/sysDictType")
public class SysDictTypeController {

    private final SysDictTypeService sysDictTypeService;

    @Operation(summary = "分页查询字典类型列表", description = "分页查询字典类型列表", tags = "查询")
    @PostMapping("page")
    public R<Page<SysDictType>> page(@RequestBody SysDictTypeReq sysDictTypeReq) {
        Page<SysDictType> page = new Page<>(sysDictTypeReq.getPageNum(), sysDictTypeReq.getPageSize());
        LambdaQueryWrapper<SysDictType> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictType>()
                .like(StringUtils.hasText(sysDictTypeReq.getDictName()), SysDictType::getDictName, sysDictTypeReq.getDictName())
                .eq(StringUtils.hasText(sysDictTypeReq.getDictType()), SysDictType::getDictType, sysDictTypeReq.getDictType())
                .eq(sysDictTypeReq.getStatus() != null, SysDictType::getStatus, sysDictTypeReq.getStatus());
        if (sysDictTypeReq.getDateRange() != null && sysDictTypeReq.getDateRange().length == 2) {
            lambdaQueryWrapper.between(SysDictType::getCreateTime, sysDictTypeReq.getDateRange()[0], sysDictTypeReq.getDateRange()[1]);
        }
        return R.ok(sysDictTypeService.page(page, lambdaQueryWrapper));
    }

    @Operation(summary = "查询字典类型列表", description = "查询字典类型列表", tags = "查询")
    @GetMapping("list")
    public R<List<SysDictType>> list() {
        return R.ok(sysDictTypeService.list());
    }

    @Operation(summary = "根据id查询字典类型信息", description = "根据id查询字典类型信息", tags = "查询")
    @GetMapping("{id}")
    public R<SysDictType> getById(@PathVariable("id") Long id) {
        return R.ok(sysDictTypeService.getById(id));
    }

    @Operation(summary = "新增字典类型", description = "新增字典类型", tags = "新增")
    @PostMapping()
    public R<Boolean> add(@RequestBody @Validated SysDictType sysRole) {
        return R.ok(sysDictTypeService.save(sysRole));
    }

    @Operation(summary = "编辑字典类型", description = "编辑字典类型", tags = "编辑")
    @PutMapping
    public R<Boolean> edit(@RequestBody SysDictType sysDictType) {
        return R.ok(sysDictTypeService.updateById(sysDictType));
    }

    @Operation(summary = "根据ids批量删除字典类型", description = "根据ids批量删除字典类型", tags = "删除")
    @DeleteMapping("{ids}")
    public R<Boolean> deleteByIds(@PathVariable("ids") String ids) {
        String[] split = ids.split(",");
        return R.ok(sysDictTypeService.removeByIds(Arrays.asList(split)));
    }

    @Operation(summary = "导出" , description = "导出" , tags = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<SysDictType> list = sysDictTypeService.list();
        EasyExcelUtil.simpleWrite(response, "字典类型", SysDictType.class, list, "字典类型");
    }
}
